#include "minimal.h"
#include "minimal2.h"


int screen_size;
unsigned short* screenbuffer = NULL;

int keymap[65536];
unsigned long keys = 0;
pthread_t accel;
int sens,y_def;
bool running;


char *cfile;
char *loadst;
char *file;
bool fullscreen;



void hildon_init2(int *argc, char ***argv,char *fl,char* lst,bool fscreen)
{
	printf("maemo init - hildon_init2\n");
fullscreen=fscreen;
	file=fl;
	cfile=new char[255];
	sprintf (cfile, "%s.save.%s", file,lst);
	if (Config.PsxOut)printf ("Save file:%s\n",cfile);
	//cfile="/home/opt/psx4m/savestate";
	
	

	// read key config
	
	FILE* pFile;
	pFile = fopen("/opt/psx4m/keys", "r"); // assume the file exists and has data
	int ch;
	for (int i=0;i<65536;i++)
		keymap[i]=164;
	if (NULL != pFile) {
		for(int i=0;i<21;i++){
			fscanf(pFile, "%i",&ch);
			keymap[ch]=i;
		}
		fclose(pFile);
	}
	
		
}


	
void read_x11_keys(){
if (x11Window==0)return;
			//XSelectInput(x11Display, x11Window,  ExposureMask|ButtonPressMask|KeyPressMask);
			XEvent event;		/* the XEvent declaration !!! */
			KeySym key2;		/* a dealie-bob to handle KeyPress Events */	

			
while(XPending(x11Display)>0){

	XNextEvent(x11Display,&event);

			//XNextEvent(x11Display, &event);
		
			//printf("%i",event.type);

	
		if (event.type==2||event.type==3)
		{
			s32 key=0;
switch(keymap[event.xkey.keycode]){
	case -1:
		return; break;
	case 0:
			key = GP2X_LEFT;break;
	case 1:
			key = GP2X_RIGHT;break;
	case 2:
			key = GP2X_UP;break;
	case 3:
			key = GP2X_DOWN;break;
	case 4:
			key = GP2X_B;break;
	case 5:
			key = GP2X_X;break;
	case 6:
			key = GP2X_Y;break;
	case 7:
			key = GP2X_A;break;
	case 8:
			key = GP2X_SELECT;break;
	case 9:
			key = GP2X_START;break;
	case 10:
			key = GP2X_VOL_DOWN;break;
	case 11:
			key = GP2X_VOL_UP;break;
	case 12:
			key = GP2X_L;break;
	case 13:
			key = GP2X_R;break;
	case 14:
		running=false;exit (0);break;
	case 15:
			if (event.type==2){
			keys |= GP2X_LEFT;
			keys |= GP2X_UP;
			}else if (event.type==3){
			keys &= ~GP2X_LEFT;
			keys &= ~GP2X_UP;
			}
		key = -1;			return ;			break;
	case 16:
			if (event.type==2){
			keys |= GP2X_RIGHT;
			keys |= GP2X_UP;
			}else if (event.type==3){
			keys &= ~GP2X_RIGHT;
			keys &= ~GP2X_UP;
			}
		key = -1;			return ;			break;
	case 17:
			if (event.type==2){
			keys |= GP2X_LEFT;
			keys |= GP2X_DOWN;
			}else if (event.type==3){
			keys &= ~GP2X_LEFT;
			keys &= ~GP2X_DOWN;
			}
		key = -1;			return ;			break;
	case 18:
			if (event.type==2){
			keys |= GP2X_RIGHT;
			keys |= GP2X_DOWN;
			}else if (event.type==3){
			keys &= ~GP2X_RIGHT;
			keys &= ~GP2X_DOWN;
			}
		key = -1;			return ;			break;
	case 19:
		SaveState(cfile);
		key = -1;			return ;			break;
	break;
	case 20:
		LoadState(cfile);
		key = -1;			return ;			break;
	break;
}
			
if (event.type==2)
keys |= key;
else
keys &= ~key;

		}
}	
}

			

unsigned long gp2x_joystick_read(void)
{
	//printf("gp2x_joystick_read\n");
	/* process GTK+ events */
	//read_x11_keys();
read_x11_keys();

	return keys;
}

void gp2x_video_RGB_clearscreen16(void)
{
	#ifndef OPENGL_ESM
	if (gp2x_screen16 && screen_size)
		memset(gp2x_screen16, 0, screen_size);
#endif
}

void updateScreen()
{
	
}

void gp2x_printfchar15(gp2x_font *f, unsigned char c)
{
}


// original code had copied from picodrive for maemo project
static void *read_accelerometer(void*) {

	while (running){
	int accelstates = 0;
	int x, y, z;
	FILE* f = fopen( "/sys/class/i2c-adapter/i2c-3/3-001d/coord", "r" );
	if( !f ) {printf ("err in accel");continue;}
	fscanf( f, "%d %d %d", &x, &y, &z );
	fclose( f );

	
		
		if( x > sens ) keys |= GP2X_LEFT;
		else if( x < -sens ) keys |= GP2X_RIGHT;
		else {keys &= ~GP2X_LEFT;keys &= ~GP2X_RIGHT;}
		
		y+=y_def;
		if( y > sens )keys |= GP2X_UP;
		else if( y < -sens ) keys |= GP2X_DOWN; 
		else {keys &= ~GP2X_DOWN;keys &= ~GP2X_UP;}
		
		/*if( z > sens ) keys |= GP2X_A;
		else if( z < -sens ) keys |= GP2X_B;
		else {keys &= ~GP2X_A;keys &= ~GP2X_B;}*/
		
usleep (500);
		}
}
